﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Enter.Services.DataCache;
using Enter.Services.Mail;
using Enter.Mvc.Base.Controllers;
using System.Web.Mvc;
using Enter.Mvc.Mall.Model;
using Enter.Mvc.Mall.Model.User;
using Enter.Config;
using Enter.Common;
using Newtonsoft.Json;
using Enter.Entity;
using Enter.Mvc.Mall.Model.Trade;
using Enter.Mvc.Mall.Model.Order;

namespace Enter.Mvc.Mall.Controllers
{
  public  class TradeController : BaseControllers
    {
        private readonly ICache icache;
        private readonly IMail imail;

        public TradeController(ICache cache, IMail mail)
        {
            this.icache = cache;
            this.imail = mail;
         
        }
        public ActionResult list_bought_items()
        {
         

            return View();
        }
      [HttpPost]
        public ActionResult GetUserOrder(OrderSearthModel Model)
        { 
           int uID=UserInfo.CurrentUser.UserID;
         
           EnumOrderStatus orderstatus = (EnumOrderStatus)Model.auctionStatus;
           EnumCommentStatus commentstatus = (EnumCommentStatus)Model.commentStatus;
           string prodtitle = Model.auctionTitle;
           string OrderTimeBegin = Model.bizOrderTimeBegin;
           string OrderTimeEnd =Model.bizOrderTimeEnd;
            int pageSize=10;
            int currentIndex = Model.currentIndex;
             int totalPages=0,   totalCount=0;


           List<T301_Order_MainEntity> orderlist= imail.GetOrderByUid(uID, orderstatus, commentstatus, prodtitle, OrderTimeBegin, OrderTimeEnd, pageSize, currentIndex, out  totalPages, out  totalCount);
        List<OrderModel> list=new List<OrderModel>();
        if (orderlist != null)
        {
            foreach (T301_Order_MainEntity entity in orderlist)
            {
                OrderModel model = new OrderModel();
                model.ID = entity.OMID;
                model.OrderNO = entity.OMOrderNO;
                model.AMOUNT = entity.OAMOUNT;
                model.DisAmount = entity.ODisAmount;
                model.GoodAmount = entity.OGoodAmount;
                model.ISShopComment = entity.ISShopComment;
                model.ISUserComment = entity.ISUserComment;
                model.SNO = entity.SNO;
                model.ShopName = entity.ShopName;
                model.STATE = entity.OSTATE;
                model.YunFei = entity.OYunFei;
                model.AddTime = entity.OAddTime.ToString("yyyy-MM-dd HH:mm");
                model.items = new List<Order_ItemsModel>();
                foreach (T302_Order_ItemsEntity item in entity.items)
                {
                    Order_ItemsModel itemmodel = new Order_ItemsModel();
                    itemmodel.ID = item.ID;
                    itemmodel.GID = item.GID;

                    itemmodel.GItemID = item.GItemID;
                    itemmodel.ColorName = item.ColorName;
                    itemmodel.SizeName = item.SizeName;
                    if (string.IsNullOrEmpty(item.ColorName))
                    {
                        itemmodel.ColorName = "";
                    }
                    if (string.IsNullOrEmpty(item.SizeName))
                    {
                        itemmodel.SizeName = "";
                    }
                    itemmodel.ProdTitle = item.ProdTitle;
                    itemmodel.OPRICE = item.OPRICE;
                    itemmodel.ONUM = item.ONUM;
                    itemmodel.ImgUrl = ImgPathConfig.productListServerPath + item.ImgUrl;
                    itemmodel.SmallImgUrl = ImgPathConfig.productInfoServerPath + item.SmallImgUrl;
                    model.items.Add(itemmodel);
                }
                list.Add(model);
            }
        }

        if (currentIndex > totalPages)
        {
            currentIndex = totalPages;
        }

        object obj = new { list = list, totalPages = totalPages, currentIndex = currentIndex };
             string result = JavaScriptConvert.SerializeObject(obj);
             return Content(result);
        }
    }
}
